package com.apollo.springboot.config;

import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: LiuWei
 * @date: Create in 14:50 2018/08/09
 */
@Component
public class RefreshConfig {

    @Autowired
    private org.springframework.cloud.context.scope.refresh.RefreshScope refreshScope;

    private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);

    @Autowired
    RedisConfig redisConfig;

    @ApolloConfigChangeListener({"MYTEST"})
    public void onChange(ConfigChangeEvent changeEvent) {
        logger.info("config change-----------------");
        for (String changedKey : changeEvent.changedKeys()) {
            if (changedKey.startsWith("redis.cache")) {

                logger.info("before refresh {}", redisConfig.toString());
                refreshScope.refresh("redisConfig");

                logger.info("after refresh {}", redisConfig.toString());


                break;
            }
        }
    }
}
